#define _CRT_SECURE_NO_WARNINGS 1


#include<stdio.h>
#include<stdlib.h>
#include<math.h>

struct ListNode
{
    int val;
    ListNode* next;
};

typedef struct ListNode ListNode;

int ListSize(ListNode* phead)
{
    int size = 0;
    ListNode* pcur = phead;
    while (pcur)
    {
        size++;
        pcur = pcur->next;
    }
    return size;
}

int getDecimalValue(struct ListNode* head)
{
    int size = ListSize(head) - 1;
    int ret = 0;
    ListNode* pcur = head;
    while (pcur)
    {
        int x = (int)(pow(2, size--)) * pcur->val;
        ret += x;
        pcur = pcur->next;
    }
    return ret;
}

ListNode* BuyNode(int x)
{
    ListNode* node = (ListNode*)malloc(sizeof(ListNode));
    if (node == NULL)
    {
        exit(1);
    }
    node->val = x;
    node->next = NULL;
}

void ListPushBack(ListNode** pphead, int x)
{
    ListNode* NewNode = BuyNode(x);
    if (*pphead == NULL)
    {
        *pphead = NewNode;
    }
    else
    {
        ListNode* pcur = *pphead;
        while (pcur->next)
        {
            pcur = pcur->next;
        }
        pcur->next = NewNode;
    }
}

int main()
{
    ListNode* plist = NULL;
    ListPushBack(&plist, 1);
    ListPushBack(&plist, 0);
    ListPushBack(&plist, 1);
    int ret = getDecimalValue(plist);

    return 0;
}